package com.iflytek.uoamp.notify.core.adapter;

import java.text.MessageFormat;

import com.alibaba.fastjson.JSONObject;
import com.iflytek.uoamp.common.notify.plugins.AbstractPlugin;
import com.iflytek.uoamp.common.notify.util.NotifyUtils;
import com.iflytek.uoamp.notify.core.protocol.NotificationContent;
import org.slf4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.iflytek.uoamp.common.notify.constant.ConstField;
import com.iflytek.uoamp.common.notify.enumerate.Type;
import com.iflytek.uoamp.common.notify.obj.Notification;
import com.iflytek.uoamp.common.notify.obj.NotificationType;
import com.iflytek.uoamp.common.notify.util.LogbackInstance;
import com.iflytek.uoamp.notify.core.handler.ResponseHelper;
import com.iflytek.uoamp.notify.core.protocol.DirectNotification;
import com.iflytek.uoamp.notify.core.protocol.TemplateNotification;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class ProtoclAdapter extends SimpleChannelInboundHandler<String>{

	private static final Logger logger = LogbackInstance.getInstance().getLogger(ProtoclAdapter.class);
	
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
		try{
		    logger.debug("Recieved Package: " + msg);
			NotificationContent notificationContent = JSONObject.parseObject(msg, NotificationContent.class);

			//notification
			NotificationType notificationType = JSON.parseObject(msg, NotificationType.class);
			Type packType = Type.getType(notificationType.getType());
			Notification notification = null;

			if (packType == Type.UNKNOWN) {
				logger.error(MessageFormat.format(ConstField.ILLEGALNOTIFICATIONTYPE, notificationType.getType()));
				ResponseHelper.send(ctx, ResponseHelper.getResponse(ctx, MessageFormat.format(ConstField.ILLEGALNOTIFICATIONTYPE, notificationType.getType())));
				return;
			} else if (packType == Type.FULL_TEMPLATE || packType == Type.ID_ONLY) {
				notification = JSON.parseObject(notificationContent.getNotificationDesc(), TemplateNotification.class);
			} else {
				notification = JSON.parseObject(notificationContent.getNotificationDesc(), DirectNotification.class);
			}
			notificationContent.setNotification(notification);

			//plugin
			AbstractPlugin plugin = NotifyUtils.getPlugin(notificationContent.getPluginType());
			notificationContent.setPlugin(plugin);
			NotifyTask.getInstance().addTask(notificationContent);

			/*NotificationType notificationType = JSON.parseObject(msg, NotificationType.class);
            ActionContext actionContext = new ActionContext();

            // ltzeng 根据type字段获得通知发送方式,根据发送方式构造不同的类型的Notification
            Type packType = Type.getType(notificationType.getType());
            Notification notification = null;

            if (packType == Type.UNKNOWN) {
                logger.error(MessageFormat.format(ConstField.ILLEGALNOTIFICATIONTYPE, notificationType.getType()));
                ResponseHelper.send(ctx, ResponseHelper.getResponse(ctx, MessageFormat.format(ConstField.ILLEGALNOTIFICATIONTYPE, notificationType.getType())));
                return;
            } else if (packType == Type.FULL_TEMPLATE || packType == Type.ID_ONLY) {
                notification = JSON.parseObject(msg, TemplateNotification.class);
            } else {
                notification = JSON.parseObject(msg, DirectNotification.class);
            }

			actionContext.setRequestPackage(msg);
			actionContext.setNotification(notification);
			AsyncTask.getInstance().addTask(actionContext);*/
		}catch(JSONException e){
			logger.error("Parse recieved package error: " + msg);
			logger.error(MessageFormat.format(ConstField.ILLEGALJSONSTRING, e.getMessage()), e);
			ResponseHelper.send(ctx, ResponseHelper.getResponse(ctx, MessageFormat.format(ConstField.ILLEGALJSONSTRING, e.getMessage())));
		} catch(Exception e) {
			logger.error("Parse recieved package error: " + msg);
		    logger.error(MessageFormat.format(ConstField.UNKNOWNERROR, e.getMessage()), e);
		    ResponseHelper.send(ctx, ResponseHelper.getResponse(ctx, MessageFormat.format(ConstField.UNKNOWNERROR,e.getMessage())));
		}
		
		ResponseHelper.send(ctx, ResponseHelper.getResponse(ctx, "请求提交成功!"));
	}
}